
package kaizone.haining.chaobohui.widget;

import java.util.List;

import kaizone.haining.chaobohui.R;
import kaizone.haining.chaobohui.activity.DetailActivity;
import kaizone.haining.chaobohui.json.DetailDO;
import kaizone.haining.chaobohui.json.Item.PicModel;
import kaizone.haining.chaobohui.json.JSONResponse;
import kaizone.haining.chaobohui.login.UserControl;
import kaizone.haining.chaobohui.net.DataControl;
import kaizone.haining.chaobohui.setup.Shareter;
import kaizone.haining.chaobohui.setup.Shareter.ShareEntry;
import kaizone.haining.chaobohui.setup.XinLangWeiBo;
import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

public class UsersActionBar extends LinearLayout implements DataControl.IDataUpdateListener,
        OnClickListener {
    private ImageView mFollowsView;

    private ImageView mCheckinsView;

    private ImageView mShareView;

    private ImageView mFavoriteView;

    private DataControl mDataControl;

    private UserControl mUserControl;

    private int mActivityId;

    private int mSubjectId;

    private int mNewsId;

    private String mUrlFollowsActivityString;

    private String mUrlCheckinActivityString;

    private String mUrlFavoriteSubjectString;

    private Object mShareObject;

    private int mFlag;

    public UsersActionBar(Context context) {
        super(context);
    }

    public UsersActionBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        mDataControl = new DataControl(getContext());
        mDataControl.addChangeListener(this);

        mUserControl = new UserControl(getContext());
    };

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mFollowsView = (ImageView) findViewById(R.id.img01);
        mCheckinsView = (ImageView) findViewById(R.id.img02);
        mShareView = (ImageView) findViewById(R.id.img03);
        mFavoriteView = (ImageView) findViewById(R.id.img04);

        mFollowsView.setOnClickListener(this);
        mCheckinsView.setOnClickListener(this);
        mShareView.setOnClickListener(this);
        mFavoriteView.setOnClickListener(this);

        mFollowsView.setVisibility(View.GONE);
        mCheckinsView.setVisibility(View.GONE);
        mShareView.setVisibility(View.GONE);
        mFavoriteView.setVisibility(View.GONE);
    }

    public void addFollows() {
        mFollowsView.setVisibility(View.VISIBLE);
    }

    public void addCheckins() {
        mCheckinsView.setVisibility(View.VISIBLE);
    }

    public void addShare() {
        mShareView.setVisibility(View.VISIBLE);
    }

    public void addFavorite() {
        mFavoriteView.setVisibility(View.VISIBLE);
    }

    @Override
    public Object backParse(String datastring, String requeststring) {
        if (TextUtils.isEmpty(datastring))
            return null;

        JSONResponse response = null;
        if (requeststring.equals(mUrlFollowsActivityString)) {
            response = JSONResponse.parseJsonString(datastring);
        }//
        else if (requeststring.equals(mUrlCheckinActivityString)) {
            response = JSONResponse.parseJsonString(datastring);
        }//
        else if (requeststring.equals(mUrlFavoriteSubjectString)) {
            response = JSONResponse.parseJsonString(datastring);
        }
        return response;
    }

    @Override
    public void foreUpdate(Object result, String requeststring) {
        if (result == null)
            return;
        if (requeststring.equals(mUrlFollowsActivityString)) {
            if (result instanceof JSONResponse) {
                JSONResponse response = (JSONResponse) result;
                if (response.success) {
                    Toast.makeText(getContext(), "关注成功", 800).show();
                } else {
                    if (TextUtils.isEmpty(response.code))
                        return;
                    if (response.code.equals(JSONResponse.CODE_ILL_ARGS)) {
                        DetailDO.prompt(getContext(), response.detail);
                    }
                }
            }

        }//
        else if (requeststring.equals(mUrlCheckinActivityString)) {
            if (result instanceof JSONResponse) {
                JSONResponse response = (JSONResponse) result;
                if (response.success) {
                    Toast.makeText(getContext(), "签到成功", 800).show();
                } else {
                    if (TextUtils.isEmpty(response.code))
                        return;
                    if (response.code.equals(JSONResponse.CODE_ILL_ARGS)) {
                        DetailDO.prompt(getContext(), response.detail);
                    }
                }
            }
        }//
        else if (requeststring.equals(mUrlFavoriteSubjectString)) {
            if (result instanceof JSONResponse) {
                JSONResponse response = (JSONResponse) result;
                if (response.success) {
                    Toast.makeText(getContext(), "收藏成功", 800).show();
                } else {
                    if (TextUtils.isEmpty(response.code))
                        return;
                    if (response.code.equals(JSONResponse.CODE_ILL_ARGS)) {
                        DetailDO.prompt(getContext(), response.detail);
                    }
                }
            }
        }
    }

    @Override
    public void exception(Exception e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onClick(View v) {
        if (v == mFollowsView) {
            follow();
        }//
        else if (v == mCheckinsView) {
            checkin();
        }//
        else if (v == mShareView) {
            share();
        }//
        else if (v == mFavoriteView) {
            favorite();
        }
    }

    public void follow() {
        if (mFlag == DetailActivity.FLAG_HUODONG) {
            requestFollowActivity();
        }//
    }

    public void checkin() {
        if (mFlag == DetailActivity.FLAG_HUODONG) {
            requestCheckinActivity();
        }//
    }

    public void share() {
        if (mShareObject == null)
            return;
        if (mShareObject instanceof ShareEntry) {
            ((ShareEntry) mShareObject).flag = mFlag;
        }
        new ShareDialog(getContext(), mShareObject).show();
    }

    private void favorite() {
        if (mFlag == DetailActivity.FLAG_OPENWEB) {
            requestFollowSubject();
        }
    }

    public void requestFollowSubject() {
        mUrlFavoriteSubjectString = mUserControl.requestFollowSubject(mSubjectId);
        mDataControl.exec(mUrlFavoriteSubjectString);
    }

    public void requestFollowActivity() {
        mUrlFollowsActivityString = mUserControl.requestFollowActivity(mActivityId);
        mDataControl.exec(mUrlFollowsActivityString);
    }

    public void requestCheckinActivity() {
        mUrlCheckinActivityString = mUserControl.requestCheckinActivityHasLocation(mActivityId);
        mDataControl.exec(mUrlCheckinActivityString);
    }

    public void setFlag(int flag) {
        mFlag = flag;
    }

    public int getActivityId() {
        return mActivityId;
    }

    public void setActivityId(int activityId) {
        this.mActivityId = activityId;
    }

    public int getSubjectId() {
        return mSubjectId;
    }

    public void setSubjectId(int subjectId) {
        this.mSubjectId = subjectId;
    }

    public void setNewsId(int newsId) {
        mNewsId = newsId;
    }

    public void setShareObject(Object obj) {
        mShareObject = obj;
    }

    public static void attchShareData(Activity activity, String status, String imageUrl) {
        if (activity instanceof DetailActivity) {
            UsersActionBar bar = ((DetailActivity) activity).getUsersActionBar();
            if (bar == null)
                return;
            ShareEntry entry = Shareter.edit(status, imageUrl);
            bar.setShareObject(entry);
        }
    }

    public static void attchShareData(Activity activity, String status, List<PicModel> picmodels) {
        if (activity instanceof DetailActivity) {
            UsersActionBar bar = ((DetailActivity) activity).getUsersActionBar();
            if (bar == null)
                return;
            String imageUrl = null;
            if (picmodels == null || picmodels.isEmpty()) {
                imageUrl = null;
            }
            PicModel picModel = picmodels.get(0);
            if (picModel == null)
                imageUrl = null;
            imageUrl = picModel.url;
            ShareEntry entry = Shareter.edit(status, imageUrl);
            bar.setShareObject(entry);
        }
    }

    public static void attchShareData(Activity activity, String title, String content,
            String imageUrl) {
        if (activity instanceof DetailActivity) {
            UsersActionBar bar = ((DetailActivity) activity).getUsersActionBar();
            if (bar == null)
                return;
            ShareEntry entry = Shareter.edit(title, content, imageUrl);
            bar.setShareObject(entry);
        }
    }

    public static void attchShareData(Activity activity, String title, String content,
            PicModel picmodel) {
        String imageUrl;
        if (picmodel == null) {
            imageUrl = "";
        }
        imageUrl = picmodel.url;
        attchShareData(activity, title, content, imageUrl);
    }

    public static void attchShareData(Activity activity, String title, String content,
            List<PicModel> picmodels) {
        String imageUrl;
        if (picmodels == null || picmodels.isEmpty()) {
            imageUrl = "";
        } else {
            PicModel picmodel = picmodels.get(0);
            if (picmodel == null) {
                imageUrl = "";
            }
            imageUrl = picmodel.url;
        }
        attchShareData(activity, title, content, imageUrl);
    }

    public static class NewToask extends Toast {

        private Context mContext;

        private ImageView mImageView;

        public NewToask(Context context) {
            super(context);
            mContext = context;
            mImageView = new ImageView(mContext);
            setView(mImageView);
        }

        public void setImageResource(int resId) {
            mImageView.setImageResource(resId);
        }

    }

}
